package Testdemo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:周次煜
 * Date: 2024-04-24
 * Time：14:32
 */

import java.util.*;

class Direc {
    //行
    int row;
    // 列
    int col;
}


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N=in.nextInt();
        int M=in.nextInt();
        int[][]array=new int[N][M];
        for(int i=0; i < N; i++) {
            for(int j=0 ; j < M ; j++) {
                array[i][j]=in.nextInt();
            }
        }

        Stack<Direc>  stack=new Stack<>();
        Stack<Direc> prinStack=new Stack<>();

        boolean b=func(array,stack,0,0,N,M);
        if(b) {
            display(stack,prinStack);
        } else {
            System.out.println("无法走出迷宫！");
        }
    }




    private static boolean func(int[][]array,
           Stack<Direc>  stack ,int x,int y ,int N,int M) {

        // 入栈

        Direc dir =new Direc();
        dir.row=x;
        dir.col=y;


        stack.push(dir);
        // 先判断是否合法
        if(checkIndex(array,x,y,N,M)) {

            if (x==N-1 && y==M-1) {
                return  true;
            }

            array[x][y] = 2;
            // 上

            boolean b1 = func(array,  stack, dir.row-1, y, N, M);
            if (b1) {
                return true;
            }

            // 下
            boolean b2 = func(array, stack, dir.row+1 , y , N, M);
            if (b2) {
                return true;
            }

            // 左

            boolean b3 = func(array,  stack, x, dir.col - 1, N, M);
            if (b3) {
                return true;
            }

            // 右

            boolean b4 = func(array, stack, x, dir.col + 1, N, M);
            if (b4) {
                return true;
            }

        }
        stack.pop();
        return false;
    }

    private static boolean checkIndex(int[][]array,
                                      int x,int y,int N,int M) {
        if(x < N && x >= 0 &&
                y < M && y >= 0 && array[x][y] == 0) {
            return true;
        }

        return false;
    }


    private static void display(Stack<Direc> stack
            ,Stack<Direc> prinStack) {
        while(!stack.isEmpty()) {
            prinStack.push(stack.pop());
        }
        while(!prinStack.isEmpty()) {

           Direc d= prinStack.pop();
            System.out.println("("+d.row
                    +","+d.col+")");
        }
    }
}